"""initial schema

Revision ID: 0001_init
Revises:
Create Date: 2025-08-11 21:07:03.701363

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel.sql.sqltypes
from app.model.chat.chat_history import ChatStatus
from app.model.mcp.mcp import McpType
from app.model.mcp.mcp import Status
from app.model.mcp.mcp_env import Status
from app.model.mcp.mcp_user import McpType
from app.model.mcp.mcp_user import Status
from app.model.provider.provider import VaildStatus
from app.model.user.admin import Status
from app.model.user.key import KeyStatus
from app.model.user.role import RoleType
from app.model.user.user import Status
from app.model.user.user_credits_record import CreditsChannel
from sqlalchemy_utils.types import ChoiceType

# revision identifiers, used by Alembic.
revision: str = "0001_init"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "admin",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("email", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("password", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("status", ChoiceType(choices=Status, impl=sa.SmallInteger()), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "admin_role",
        sa.Column("admin_id", sa.Integer(), nullable=False),
        sa.Column("role_id", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("admin_id", "role_id"),
    )
    op.create_table(
        "category",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(length=64), nullable=False),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(length=128), nullable=False),
        sa.Column("priority", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "chat_history",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("task_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("question", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("language", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("model_platform", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("model_type", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("api_key", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("api_url", sa.String(length=500), nullable=True),
        sa.Column("max_retries", sa.Integer(), nullable=False),
        sa.Column("file_save_path", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("installed_mcp", sa.JSON(), nullable=False),
        sa.Column("project_name", sa.String(length=128), nullable=True),
        sa.Column("summary", sa.String(length=1024), nullable=True),
        sa.Column("tokens", sa.Integer(), server_default="0", nullable=True),
        sa.Column("spend", sa.Float(), server_default="0", nullable=True),
        sa.Column("status", ChoiceType(choices=ChatStatus, impl=sa.SmallInteger()), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_chat_history_task_id"), "chat_history", ["task_id"], unique=True)
    op.create_index(op.f("ix_chat_history_user_id"), "chat_history", ["user_id"], unique=False)
    op.create_table(
        "chat_snapshot",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), server_default=sa.text("0"), nullable=True),
        sa.Column("api_task_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("camel_task_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("browser_url", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("image_path", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_chat_snapshot_api_task_id"), "chat_snapshot", ["api_task_id"], unique=False)
    op.create_index(op.f("ix_chat_snapshot_camel_task_id"), "chat_snapshot", ["camel_task_id"], unique=False)
    op.create_table(
        "chat_step",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("task_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("step", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("data", sa.JSON(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_chat_step_task_id"), "chat_step", ["task_id"], unique=False)
    op.create_table(
        "config",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("config_name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("config_value", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("config_group", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id", "config_name", name="uix_user_id_config_name"),
    )
    op.create_index(op.f("ix_config_config_group"), "config", ["config_group"], unique=False)
    op.create_index(op.f("ix_config_user_id"), "config", ["user_id"], unique=False)
    op.create_table(
        "plan",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("plan_key", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("price_month", sa.Float(), nullable=False),
        sa.Column("price_year", sa.Float(), nullable=False),
        sa.Column("daily_credits", sa.Integer(), nullable=False),
        sa.Column("monthly_credits", sa.Integer(), nullable=False),
        sa.Column("storage_limit", sa.Integer(), nullable=False),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("is_active", sa.Boolean(), nullable=False),
        sa.Column("extra_config", sa.JSON(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_plan_name"), "plan", ["name"], unique=True)
    op.create_index(op.f("ix_plan_plan_key"), "plan", ["plan_key"], unique=True)
    op.create_table(
        "provider",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("provider_name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("model_type", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("api_key", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("endpoint_url", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("encrypted_config", sa.JSON(), nullable=True),
        sa.Column("prefer", sa.Boolean(), server_default=sa.text("false"), nullable=True),
        sa.Column(
            "is_vaild",
            ChoiceType(choices=VaildStatus, impl=sa.SmallInteger()),
            server_default=sa.text("1"),
            nullable=True,
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_provider_user_id"), "provider", ["user_id"], unique=False)
    op.create_table(
        "role",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("type", ChoiceType(choices=RoleType, impl=sa.SmallInteger()), nullable=True),
        sa.Column("permissions", sa.JSON(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "user",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("stack_id", sqlmodel.sql.sqltypes.AutoString(length=255), nullable=True),
        sa.Column("username", sqlmodel.sql.sqltypes.AutoString(length=128), nullable=True),
        sa.Column("email", sqlmodel.sql.sqltypes.AutoString(length=128), nullable=False),
        sa.Column("password", sqlmodel.sql.sqltypes.AutoString(length=256), nullable=True),
        sa.Column("avatar", sqlmodel.sql.sqltypes.AutoString(length=256), nullable=False),
        sa.Column("nickname", sqlmodel.sql.sqltypes.AutoString(length=64), nullable=False),
        sa.Column("fullname", sqlmodel.sql.sqltypes.AutoString(length=128), nullable=False),
        sa.Column("work_desc", sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
        sa.Column("credits", sa.Integer(), server_default=sa.text("0"), nullable=True),
        sa.Column("last_daily_credit_date", sa.Date(), nullable=True),
        sa.Column("last_monthly_credit_date", sa.Date(), nullable=True),
        sa.Column("inviter_user_id", sa.Integer(), nullable=True),
        sa.Column("status", ChoiceType(choices=Status, impl=sa.SmallInteger()), nullable=True),
        sa.ForeignKeyConstraint(
            ["inviter_user_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("email"),
        sa.UniqueConstraint("stack_id"),
        sa.UniqueConstraint("username"),
    )
    op.create_table(
        "key",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("value", sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
        sa.Column("inner_key", sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
        sa.Column("status", ChoiceType(choices=KeyStatus, impl=sa.SmallInteger()), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_key_user_id"), "key", ["user_id"], unique=False)
    op.create_index(op.f("ix_key_value"), "key", ["value"], unique=False)
    op.create_table(
        "mcp",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("category_id", sa.Integer(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("key", sa.String(length=128), nullable=True),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("home_page", sa.String(length=1024), nullable=True),
        sa.Column("type", ChoiceType(choices=McpType, impl=sa.SmallInteger()), nullable=True),
        sa.Column("status", ChoiceType(choices=Status, impl=sa.SmallInteger()), nullable=True),
        sa.Column("sort", sa.SmallInteger(), nullable=True),
        sa.Column("server_name", sa.String(length=128), nullable=True),
        sa.Column("install_command", sa.JSON(), nullable=True),
        sa.ForeignKeyConstraint(
            ["category_id"],
            ["category.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "user_credits_record",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("invite_by", sa.Integer(), nullable=True),
        sa.Column("invite_code", sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
        sa.Column("amount", sa.Integer(), nullable=False),
        sa.Column("balance", sa.Integer(), nullable=False),
        sa.Column("channel", ChoiceType(choices=CreditsChannel, impl=sa.SmallInteger()), nullable=True),
        sa.Column("source_id", sa.Integer(), nullable=False),
        sa.Column("remark", sqlmodel.sql.sqltypes.AutoString(length=255), nullable=False),
        sa.Column("expire_at", sa.DateTime(), nullable=True),
        sa.Column("used", sa.Boolean(), server_default=sa.text("false"), nullable=True),
        sa.Column("used_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_user_credits_record_user_id"), "user_credits_record", ["user_id"], unique=False)
    op.create_table(
        "user_privacy",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("pricacy_setting", sa.JSON(), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id"),
    )
    op.create_table(
        "user_stat",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("model_type", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("download_count", sa.Integer(), nullable=False),
        sa.Column("register_count", sa.Integer(), nullable=False),
        sa.Column("task_complete_count", sa.Integer(), nullable=False),
        sa.Column("task_failed_count", sa.Integer(), nullable=False),
        sa.Column("file_download_count", sa.Integer(), nullable=False),
        sa.Column("file_generate_count", sa.Integer(), nullable=False),
        sa.Column("paid_amount_on_avg_task", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_user_stat_user_id"), "user_stat", ["user_id"], unique=False)
    op.create_table(
        "mcp_env",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("mcp_id", sa.Integer(), nullable=False),
        sa.Column("env_name", sa.String(length=128), nullable=True),
        sa.Column("env_description", sa.TEXT(), nullable=True),
        sa.Column("env_key", sa.String(length=128), nullable=True),
        sa.Column("env_default_value", sa.String(length=1024), nullable=True),
        sa.Column("env_required", sa.SmallInteger(), nullable=True),
        sa.Column("status", ChoiceType(choices=Status, impl=sa.SmallInteger()), nullable=True),
        sa.ForeignKeyConstraint(
            ["mcp_id"],
            ["mcp.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "mcp_user",
        sa.Column("deleted_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("mcp_id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("mcp_name", sa.String(length=128), nullable=True),
        sa.Column("mcp_key", sa.String(length=128), nullable=True),
        sa.Column("mcp_desc", sa.String(length=1024), nullable=True),
        sa.Column("command", sa.String(length=1024), nullable=True),
        sa.Column("args", sa.String(length=1024), nullable=True),
        sa.Column("env", sa.JSON(), nullable=True),
        sa.Column("type", ChoiceType(choices=McpType, impl=sa.SmallInteger()), nullable=True),
        sa.Column("status", ChoiceType(choices=Status, impl=sa.SmallInteger()), nullable=True),
        sa.Column("server_url", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.ForeignKeyConstraint(
            ["mcp_id"],
            ["mcp.id"],
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("mcp_user")
    op.drop_table("mcp_env")
    op.drop_index(op.f("ix_user_stat_user_id"), table_name="user_stat")
    op.drop_table("user_stat")
    op.drop_table("user_privacy")
    op.drop_index(op.f("ix_user_credits_record_user_id"), table_name="user_credits_record")
    op.drop_table("user_credits_record")
    op.drop_table("mcp")
    op.drop_index(op.f("ix_key_value"), table_name="key")
    op.drop_index(op.f("ix_key_user_id"), table_name="key")
    op.drop_table("key")
    op.drop_table("user")
    op.drop_table("role")
    op.drop_index(op.f("ix_provider_user_id"), table_name="provider")
    op.drop_table("provider")
    op.drop_index(op.f("ix_plan_plan_key"), table_name="plan")
    op.drop_index(op.f("ix_plan_name"), table_name="plan")
    op.drop_table("plan")
    op.drop_index(op.f("ix_config_user_id"), table_name="config")
    op.drop_index(op.f("ix_config_config_group"), table_name="config")
    op.drop_table("config")
    op.drop_index(op.f("ix_chat_step_task_id"), table_name="chat_step")
    op.drop_table("chat_step")
    op.drop_index(op.f("ix_chat_snapshot_camel_task_id"), table_name="chat_snapshot")
    op.drop_index(op.f("ix_chat_snapshot_api_task_id"), table_name="chat_snapshot")
    op.drop_table("chat_snapshot")
    op.drop_index(op.f("ix_chat_history_user_id"), table_name="chat_history")
    op.drop_index(op.f("ix_chat_history_task_id"), table_name="chat_history")
    op.drop_table("chat_history")
    op.drop_table("category")
    op.drop_table("admin_role")
    op.drop_table("admin")
    # ### end Alembic commands ###
